<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class sitemap extends a_base {

    public function __construct() {
        parent::__construct();
        $this->load->model('m_class');
    }

    public function index(){
        $post = $this->input->post();
        if($post !== false){
            $class_arr = $this->m_public->getAllInfo('class');
            $content = '<?xml version="1.0" encoding="UTF-8"?>';
            $content .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
            foreach($class_arr as $val){
                if($val['configid'] == 1){
                    $content .= '<url><loc>'.'http://'.$_SERVER['HTTP_HOST'].'/cn/'.$val['classlinkname'].'.html</loc>';
                }else if($val['configid'] == 2){
                    $content .= '<url><loc>'.'http://'.$_SERVER['HTTP_HOST'].'/en/'.$val['classlinkname'].'.html</loc>';
                }
                $content .= '<lastmod>'.utf8_encode(date("Y-m-d H:i:s", time())).'</lastmod>';
                $content .= '<changefreq>weekly</changefreq></url>';
            }
            $product_condition = array(
                'from' => 'product',
                'join' => array(
                    'class' => 'class.classid=product.classid'
                ),
                'column' => 'product.productid,class.configid'
            );
            $product_arr = $this->m_public->get_list($product_condition);
            foreach($product_arr as $val){
                if($val['configid'] == 1){
                    $content .= '<url><loc>'.'http://'.$_SERVER['HTTP_HOST'].'/cn/product-details-'.$val['productid'].'.html</loc>';
                }else if($val['configid'] == 2){
                    $content .= '<url><loc>'.'http://'.$_SERVER['HTTP_HOST'].'/en/product-details-'.$val['productid'].'.html</loc>';
                }
                $content .= '<lastmod>'.utf8_encode(date("Y-m-d H:i:s", time())).'</lastmod>';
                $content .= '<changefreq>weekly</changefreq></url>';
            }
            $article_condition = array(
                'from' => 'article',
                'join' => array(
                    'class' => 'class.classid=article.classid'
                ),
                'column' => 'article.articleid,class.configid'
            );
            $article_arr = $this->m_public->get_list($article_condition);
            foreach($article_arr as $val){
                if($val['configid'] == 1){
                    $content .= '<url><loc>'.'http://'.$_SERVER['HTTP_HOST'].'/cn/article-details-'.$val['articleid'].'.html</loc>';
                }else if($val['configid'] == 2){
                    $content .= '<url><loc>'.'http://'.$_SERVER['HTTP_HOST'].'/en/article-details-'.$val['articleid'].'.html</loc>';
                }
                $content .= '<lastmod>'.utf8_encode(date("Y-m-d H:i:s", time())).'</lastmod>';
                $content .= '<changefreq>weekly</changefreq></url>';
            }
            $content .= '</urlset>';
            $fp=fopen(getcwd().'/sitemap.xml','w+');
            fwrite($fp,$content);
            fclose($fp);
            redirect('sitemap/index');
        }else{
            $data = array();
            $current_dir = opendir(getcwd());    //opendir()返回一个目录句柄,失败返回false
            while(($file = readdir($current_dir)) !== false) {    //readdir()返回打开目录句柄中的一个条目
                $sub_dir = getcwd() . DIRECTORY_SEPARATOR . $file;    //构建子目录路径
                if($file == '.' || $file == '..') {
                    continue;
                } else if(is_file($sub_dir)) {
                    if($file == 'sitemap.xml'){
                        $data['file'] = $file;
                    }
                }
            }
            $this->load->view('sitemap/index',$data);
        }
    }

    public function download($filename){
        Header("Content-type: application/octet-stream");
        Header("Accept-Ranges: bytes");
        Header("Accept-Length:".filesize(getcwd().'/sitemap.xml'));
        Header("Content-Disposition: attachment; filename=sitemap.xml");
    }

}
